package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;

	public class Sprite3DRing extends Sprite
	{
		[Embed(source="mask.gif")] public var MaskGIF:Class;
		public var maskGif:Bitmap = new MaskGIF() as Bitmap;
		
		[Embed(source="color.jpg")] public var ColorJPG:Class;
		public var colorJpg:Bitmap = new ColorJPG() as Bitmap;
		
		public var blue:ColorMatrixFilter = new ColorMatrixFilter([0,0,0,0,0,
																   0,0,0,0,0,
																   0,0,1,0,0,
																   0,0,1,0,0]);
		public var green:ColorMatrixFilter = new ColorMatrixFilter([0,0,0,0,0,
																	0,1,0,0,0,
																	0,0,0,0,0,
																	0,1,0,0,0]);
		
		public var bmalpha:BitmapData = new BitmapData(40,40, true, 0x0);
		public var bmdb:BitmapData = new BitmapData(40,40, true, 0x0);
		public var bmdf:BitmapData = new BitmapData(40,40, true, 0x0);
		public var bmpb:Bitmap;
		public var bmpf:Bitmap;
		public var m:Matrix = new Matrix();
		
		private var _spinRotation:Number = 0;
		private var speed:int = 3;
		
		private var LayerReveal:Sprite = new Sprite();
		
		public function Sprite3DRing()
		{
			bmpb = new Bitmap(bmdb);
			bmpf = new Bitmap(bmdf);
			
			bmpb.x = 200;
			bmpb.y = 200;
			
			bmpf.x = 200;
			bmpf.y = 200;
			
			LayerReveal.graphics.beginFill(0xFFFFFF,.5);
			LayerReveal.graphics.drawRect(200,200,40,40);
			LayerReveal.graphics.endFill();
			
			addChild(bmpb);
			addChild(LayerReveal);
			addChild(bmpf);
			
			addEventListener(Event.ENTER_FRAME, onEnter);
		}
		
		public function onEnter(event:Event):void
		{
			spin += speed;
		}
		
		public function get spin():Number { return _spinRotation; }
		public function set spin(value:Number):void
		{
			_spinRotation = value%360;
			
			m.ty = -40*Math.floor(_spinRotation/45);
			m.tx = -40*Math.floor((_spinRotation%45)/5.625);
			
			bmdf.draw(colorJpg, m);
			bmalpha.draw(maskGif, m);
			bmalpha.applyFilter(bmalpha, bmalpha.rect, new Point(), green);
			bmdf.copyChannel(bmalpha, bmalpha.rect, new Point(), 8, 8);
			
			bmdb.draw(colorJpg, m);
			bmalpha.draw(maskGif, m);
			bmalpha.applyFilter(bmalpha, bmalpha.rect, new Point(), blue);
			bmdb.copyChannel(bmalpha, bmalpha.rect, new Point(), 8, 8);
		}
	}
}